package com.promis.widget.lettersort;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.promis.util.CollectionUtils;
import com.promis.widget.R;
import com.promis.widget.recycler.GridSpacingItemDecoration;
import com.promis.widget.recycler.RecyclerLayoutManagerHelper;

import java.util.List;

/**
 * @author: xp
 * @date: 2017/7/19
 */

public class SortAdapter extends RecyclerView.Adapter<SortAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private List<SortModel> mData;
    private String[] hotspotDatas;
    private String[] hotspotFlag;
    private Context mContext;

    public SortAdapter(Context context, String[] hotspotDatas, List<SortModel> data) {
        this.mInflater = LayoutInflater.from(context);
        this.hotspotDatas = hotspotDatas;
        this.mData = data;
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View view = mInflater.inflate(R.layout.adapter_letter_hotspot, parent, false);
            return new HotspotViewHolder(mContext, view, hotspotDatas);
        } else {
            View view = mInflater.inflate(R.layout.adapter_letter_sort_item, parent, false);
            return new ItemViewHolder(mContext, view);
        }
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        if (!CollectionUtils.isNullOrEmpty(hotspotDatas) && position == 0) {
            HotspotViewHolder itemHolder = (HotspotViewHolder) holder;

            itemHolder.tvTag.setVisibility(View.VISIBLE);
            itemHolder.tvTag.setText("热点");

            if (mOnHotspotClickListener != null) {
                itemHolder.adptHotspot.setOnItemClickListener(new HotspotAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        mOnHotspotClickListener.onItemClick(view, hotspotDatas[position]);
                    }
                });
            }
        } else {
            int mPosition = position;
            if (!CollectionUtils.isNullOrEmpty(hotspotDatas)) {
                mPosition--;
            }
            int section = getSectionForPosition(mPosition);
            ItemViewHolder itemHolder = (ItemViewHolder) holder;
            //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
            if (position == getPositionForSection(section)) {
                itemHolder.tvTag.setVisibility(View.VISIBLE);
                itemHolder.tvTag.setText(mData.get(mPosition).getLetters());
            } else {
                itemHolder.tvTag.setVisibility(View.GONE);
            }

            final int mFinalPosition = mPosition;
            if (mOnItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(holder.itemView, mData.get(mFinalPosition).getName());
                    }
                });

            }

            itemHolder.tvName.setText(this.mData.get(mPosition).getName());

//            itemHolder.tvName.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Toast.makeText(mContext, mData.get(mFinalPosition).getName(),Toast.LENGTH_SHORT).show();
//                }
//            });
        }
    }

    @Override
    public int getItemCount() {
        if (!CollectionUtils.isNullOrEmpty(hotspotDatas) && !CollectionUtils.isNullOrEmpty(mData)) {
            return mData.size() + 1;
        } else if (CollectionUtils.isNullOrEmpty(hotspotDatas) && !CollectionUtils.isNullOrEmpty(mData)) {
            return mData.size();
        } else return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (!CollectionUtils.isNullOrEmpty(hotspotDatas) && position == 0) {
            return -1;
        } else {
            return super.getItemViewType(position);
        }
    }

    //**********************itemClick************************
    public interface OnItemClickListener {
        void onItemClick(View view, String name);
    }

    private OnItemClickListener mOnItemClickListener;

    private OnItemClickListener mOnHotspotClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnHotspotClickListener(OnItemClickListener mOnHotspotClickListener) {
        this.mOnHotspotClickListener = mOnHotspotClickListener;
    }
    //**************************************************************

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static class ItemViewHolder extends ViewHolder {
        TextView tvTag, tvName;

        public ItemViewHolder(Context context, View itemView) {
            super(itemView);

            tvTag = (TextView) itemView.findViewById(R.id.tag);
            tvName = (TextView) itemView.findViewById(R.id.name);
        }
    }

    public static class HotspotViewHolder extends ViewHolder {
        private TextView tvTag;
        private RecyclerView rylvList;
        private HotspotAdapter adptHotspot;

        public HotspotViewHolder(Context context, View itemView, String[] hotspotDatas) {
            super(itemView);

            tvTag = (TextView) itemView.findViewById(R.id.tag);
            rylvList = (RecyclerView) itemView.findViewById(R.id.rylv);

            RecyclerLayoutManagerHelper.buildStaggeredGridLayoutManager(context, rylvList, 3);
            rylvList.addItemDecoration(new GridSpacingItemDecoration(context, 24));
            adptHotspot = new HotspotAdapter(context, hotspotDatas);
            rylvList.setAdapter(adptHotspot);
        }
    }

    /**
     * 提供给Activity刷新数据
     * @param list
     */
    public void updateList(List<SortModel> list, boolean hideHotspot){
        this.mData = list;
        if (hideHotspot) {
            hotspotFlag = hotspotDatas;
            hotspotDatas = null;
            notifyDataSetChanged();
        } else {
            hotspotDatas = hotspotFlag;
        }
        notifyDataSetChanged();
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的char ascii值
     */
    public int getSectionForPosition(int position) {
        return mData.get(position).getLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getItemCount(); i++) {
            if (!CollectionUtils.isNullOrEmpty(hotspotDatas)) {
                if (i != 0) {
                    String sortStr = mData.get(i - 1).getLetters();
                    char firstChar = sortStr.toUpperCase().charAt(0);
                    if (firstChar == section) {
                        return i;
                    }
                }
            } else {
                String sortStr = mData.get(i).getLetters();
                char firstChar = sortStr.toUpperCase().charAt(0);
                if (firstChar == section) {
                    return i;
                }
            }
        }
        return -1;
    }

}
